package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.common.UserContext;
import org.wmq.travelagency02.mapper.ChatUserMapper;
import org.wmq.travelagency02.model.Entity.ChatUser;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.ChatUserService;
import org.wmq.travelagency02.service.GuidebookService;
import org.wmq.travelagency02.service.TravellerService;

@RestController
@RequestMapping("/chat-user")
@RequiredArgsConstructor
public class ChatUserController {
    private final ChatUserService chatUserService;


    @PostMapping
    public Result<Void> save(@RequestBody ChatUser chatUser){
        boolean success = this.chatUserService.saveChatUser(chatUser);
        if (!success){
            return Result.error("保存失败");
        }
        return Result.ok();
    }

    @GetMapping("/{id}")
    public Result<ChatUser> getById(@PathVariable String id){
        ChatUser result = this.chatUserService.getById(id);
        if (result == null){
            return Result.error("没有找到该数据");
        }
        return Result.ok(result);
    }

    @GetMapping("/me")
    public Result<ChatUser> getByUserId(){
        ChatUser result = this.chatUserService.getByUserId(UserContext.getUserId(), UserContext.getUserType());
        if (result == null){
            Result.error("当前用户并没有聊天账户");
        }
        return Result.ok(result);

    }

    @PutMapping
    public Result<Void> update(@RequestBody ChatUser chatUser){
        boolean success = this.chatUserService.updateById(chatUser);
        if (!success){
            return Result.error("更新失败");
        }
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable String id){
        boolean success = this.chatUserService.removeChatUserById(id);
        if (!success){
            return Result.error("删除失败");
        }
        return Result.ok();
    }
}
